<!DOCTYPE html>


<html lang="zn-CH">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="What I can do is to Do My Personal Best!" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    HTML编码规范 |  小熊的博客
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="http://www.cdn.adingxiong.com/b5363a06dc744d79a94021492bcefe27" />
  
  
<link rel="stylesheet" href="/blog/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/blog/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

<link rel="alternate" href="/blog/atom.xml" title="小熊的博客" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content on">
      <section class="outer">
  <article id="post-html-standard" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  HTML编码规范
</h1>
 

    </header>
    

    
    <div class="article-meta">
      <a href="/blog/2018/12/02/html-standard/" class="article-date">
  <time datetime="2018-12-01T16:00:00.000Z" itemprop="datePublished">2018-12-02</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/blog/categories/%E5%9F%BA%E6%9C%AC%E8%A7%84%E8%8C%83/">基本规范</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">4k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">17 min</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      
      

      
      <p><a href="#1-%E5%89%8D%E8%A8%80">1 前言</a></p>
<p><a href="#2-%E4%BB%A3%E7%A0%81%E9%A3%8E%E6%A0%BC">2 代码风格</a></p>
<p>　　<a href="#21-%E7%BC%A9%E8%BF%9B%E4%B8%8E%E6%8D%A2%E8%A1%8C">2.1 缩进与换行</a></p>
<p>　　<a href="#22-%E5%91%BD%E5%90%8D">2.2 命名</a></p>
<p>　　<a href="#23-%E6%A0%87%E7%AD%BE">2.3 标签</a></p>
<p>　　<a href="#24-%E5%B1%9E%E6%80%A7">2.4 属性</a></p>
<p><a href="#3-%E9%80%9A%E7%94%A8">3 通用</a></p>
<p>　　<a href="#31-doctype">3.1 DOCTYPE</a></p>
<p>　　<a href="#32-%E7%BC%96%E7%A0%81">3.2 编码</a></p>
<p>　　<a href="#33-css%E5%92%8Cjavascript%E5%BC%95%E5%85%A5">3.3 CSS和JavaScript引入</a></p>
<p><a href="#4-head">4 head</a></p>
<p>　　<a href="#41-title">4.1 title</a></p>
<p>　　<a href="#42-favicon">4.2 favicon</a></p>
<p>　　<a href="#43-viewport">4.3 viewport</a></p>
<p><a href="#5-%E5%9B%BE%E7%89%87">5 图片</a></p>
<p><a href="#6-%E8%A1%A8%E5%8D%95">6 表单</a></p>
<p>　　<a href="#61-%E6%8E%A7%E4%BB%B6%E6%A0%87%E9%A2%98">6.1 控件标题</a></p>
<p>　　<a href="#62-%E6%8C%89%E9%92%AE">6.2 按钮</a></p>
<p>　　<a href="#63-%E5%8F%AF%E8%AE%BF%E9%97%AE%E6%80%A7-a11y">6.3 可访问性 (A11Y)</a></p>
<p><a href="#7-%E5%A4%9A%E5%AA%92%E4%BD%93">7 多媒体</a></p>
<p><a href="#8-%E6%A8%A1%E6%9D%BF%E4%B8%AD%E7%9A%84-html">8 模板中的 HTML</a></p>
<h2 id="1-前言"><a href="#1-前言" class="headerlink" title="1 前言"></a>1 前言</h2><p>HTML作为描述网页结构的超文本标记语言，在百度一直有着广泛的应用。本文档的目标是使HTML代码风格保持一致，容易被理解和被维护。</p>
<h2 id="2-代码风格"><a href="#2-代码风格" class="headerlink" title="2 代码风格"></a>2 代码风格</h2><h3 id="2-1-缩进与换行"><a href="#2-1-缩进与换行" class="headerlink" title="2.1 缩进与换行"></a>2.1 缩进与换行</h3><h4 id="强制-使用-4-个空格做为一个缩进层级，不允许使用-2-个空格-或-tab-字符。"><a href="#强制-使用-4-个空格做为一个缩进层级，不允许使用-2-个空格-或-tab-字符。" class="headerlink" title="[强制] 使用 4 个空格做为一个缩进层级，不允许使用 2 个空格 或 tab 字符。"></a>[强制] 使用 <code>4</code> 个空格做为一个缩进层级，不允许使用 <code>2</code> 个空格 或 <code>tab</code> 字符。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>first<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>second<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-每行不得超过-120-个字符。"><a href="#建议-每行不得超过-120-个字符。" class="headerlink" title="[建议] 每行不得超过 120 个字符。"></a>[建议] 每行不得超过 <code>120</code> 个字符。</h4><p>解释：</p>
<p>过长的代码不容易阅读与维护。但是考虑到 HTML 的特殊性，不做硬性要求。</p>
<h3 id="2-2-命名"><a href="#2-2-命名" class="headerlink" title="2.2 命名"></a>2.2 命名</h3><h4 id="强制-class-必须单词全字母小写，单词间以-分隔。"><a href="#强制-class-必须单词全字母小写，单词间以-分隔。" class="headerlink" title="[强制] class 必须单词全字母小写，单词间以 - 分隔。"></a>[强制] <code>class</code> 必须单词全字母小写，单词间以 <code>-</code> 分隔。</h4><h4 id="强制-class-必须代表相应模块或部件的内容或功能，不得以样式信息进行命名。"><a href="#强制-class-必须代表相应模块或部件的内容或功能，不得以样式信息进行命名。" class="headerlink" title="[强制] class 必须代表相应模块或部件的内容或功能，不得以样式信息进行命名。"></a>[强制] <code>class</code> 必须代表相应模块或部件的内容或功能，不得以样式信息进行命名。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"sidebar"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"left"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="强制-元素-id-必须保证页面唯一。"><a href="#强制-元素-id-必须保证页面唯一。" class="headerlink" title="[强制] 元素 id 必须保证页面唯一。"></a>[强制] 元素 <code>id</code> 必须保证页面唯一。</h4><p>解释：</p>
<p>同一个页面中，不同的元素包含相同的 id，不符合 id 的属性含义。并且使用 document.getElementById 时可能导致难以追查的问题。</p>
<h4 id="建议-id-建议单词全字母小写，单词间以-分隔。同项目必须保持风格一致。"><a href="#建议-id-建议单词全字母小写，单词间以-分隔。同项目必须保持风格一致。" class="headerlink" title="[建议] id 建议单词全字母小写，单词间以 - 分隔。同项目必须保持风格一致。"></a>[建议] <code>id</code> 建议单词全字母小写，单词间以 <code>-</code> 分隔。同项目必须保持风格一致。</h4><h4 id="建议-id、class-命名，在避免冲突并描述清楚的前提下尽可能短。"><a href="#建议-id、class-命名，在避免冲突并描述清楚的前提下尽可能短。" class="headerlink" title="[建议] id、class 命名，在避免冲突并描述清楚的前提下尽可能短。"></a>[建议] <code>id</code>、<code>class</code> 命名，在避免冲突并描述清楚的前提下尽可能短。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"nav"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"navigation"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">"comment"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">"com"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">"author"</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">"red"</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="强制-禁止为了-hook-脚本，创建无样式信息的-class。"><a href="#强制-禁止为了-hook-脚本，创建无样式信息的-class。" class="headerlink" title="[强制] 禁止为了 hook 脚本，创建无样式信息的 class。"></a>[强制] 禁止为了 <code>hook 脚本</code>，创建无样式信息的 <code>class</code>。</h4><p>解释：</p>
<p>不允许 class 只用于让 JavaScript 选择某些元素，class 应该具有明确的语义和样式。否则容易导致 css class 泛滥。</p>
<p>使用 id、属性选择作为 hook 是更好的方式。</p>
<h4 id="强制-同一页面，应避免使用相同的-name-与-id。"><a href="#强制-同一页面，应避免使用相同的-name-与-id。" class="headerlink" title="[强制] 同一页面，应避免使用相同的 name 与 id。"></a>[强制] 同一页面，应避免使用相同的 <code>name</code> 与 <code>id</code>。</h4><p>解释：</p>
<p>IE 浏览器会混淆元素的 id 和 name 属性， document.getElementById 可能获得不期望的元素。所以在对元素的 id 与 name 属性的命名需要非常小心。</p>
<p>一个比较好的实践是，为 id 和 name 使用不同的命名法。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">name</span>=<span class="string">"foo"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"foo"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="actionscript"><span class="comment">// IE6 将显示 INPUT</span></span></span><br><span class="line"><span class="javascript">alert(<span class="built_in">document</span>.getElementById(<span class="string">'foo'</span>).tagName);</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">`</span><br></pre></td></tr></table></figure>


<h3 id="2-3-标签"><a href="#2-3-标签" class="headerlink" title="2.3 标签"></a>2.3 标签</h3><h4 id="强制-标签名必须使用小写字母。"><a href="#强制-标签名必须使用小写字母。" class="headerlink" title="[强制] 标签名必须使用小写字母。"></a>[强制] 标签名必须使用小写字母。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>Hello StyleGuide!<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">P</span>&gt;</span>Hello StyleGuide!<span class="tag">&lt;/<span class="name">P</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="强制-对于无需自闭合的标签，不允许自闭合。"><a href="#强制-对于无需自闭合的标签，不允许自闭合。" class="headerlink" title="[强制] 对于无需自闭合的标签，不允许自闭合。"></a>[强制] 对于无需自闭合的标签，不允许自闭合。</h4><p>解释：</p>
<p>常见无需自闭合标签有input、br、img、hr等。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">"title"</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">"title"</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="强制-对-HTML5-中规定允许省略的闭合标签，不允许省略闭合标签。"><a href="#强制-对-HTML5-中规定允许省略的闭合标签，不允许省略闭合标签。" class="headerlink" title="[强制] 对 HTML5 中规定允许省略的闭合标签，不允许省略闭合标签。"></a>[强制] 对 <code>HTML5</code> 中规定允许省略的闭合标签，不允许省略闭合标签。</h4><p>解释：</p>
<p>对代码体积要求非常严苛的场景，可以例外。比如：第三方页面使用的投放系统。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>first<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>second<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>first</span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>second</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h4 id="强制-标签使用必须符合标签嵌套规则。"><a href="#强制-标签使用必须符合标签嵌套规则。" class="headerlink" title="[强制] 标签使用必须符合标签嵌套规则。"></a>[强制] 标签使用必须符合标签嵌套规则。</h4><p>解释：</p>
<p>比如 div 不得置于 p 中，tbody 必须置于 table 中。</p>
<p>详细的标签嵌套规则参见<a href="http://www.cs.tut.fi/~jkorpela/html5.dtd" target="_blank" rel="noopener">HTML DTD</a>中的 <code>Elements</code> 定义部分。</p>
<h4 id="建议-HTML-标签的使用应该遵循标签的语义。"><a href="#建议-HTML-标签的使用应该遵循标签的语义。" class="headerlink" title="[建议] HTML 标签的使用应该遵循标签的语义。"></a>[建议] <code>HTML</code> 标签的使用应该遵循标签的语义。</h4><p>解释：</p>
<p>下面是常见标签语义</p>
<ul>
<li>p - 段落</li>
<li>h1,h2,h3,h4,h5,h6 - 层级标题</li>
<li>strong,em - 强调</li>
<li>ins - 插入</li>
<li>del - 删除</li>
<li>abbr - 缩写</li>
<li>code - 代码标识</li>
<li>cite - 引述来源作品的标题</li>
<li>q - 引用</li>
<li>blockquote - 一段或长篇引用</li>
<li>ul - 无序列表</li>
<li>ol - 有序列表</li>
<li>dl,dt,dd - 定义列表</li>
</ul>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>Esprima serves as an important <span class="tag">&lt;<span class="name">strong</span>&gt;</span>building block<span class="tag">&lt;/<span class="name">strong</span>&gt;</span> for some JavaScript language tools.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Esprima serves as an important <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">"strong"</span>&gt;</span>building block<span class="tag">&lt;/<span class="name">span</span>&gt;</span> for some JavaScript language tools.<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h4 id="建议-在-CSS-可以实现相同需求的情况下不得使用表格进行布局。"><a href="#建议-在-CSS-可以实现相同需求的情况下不得使用表格进行布局。" class="headerlink" title="[建议] 在 CSS 可以实现相同需求的情况下不得使用表格进行布局。"></a>[建议] 在 <code>CSS</code> 可以实现相同需求的情况下不得使用表格进行布局。</h4><p>解释：</p>
<p>在兼容性允许的情况下应尽量保持语义正确性。对网格对齐和拉伸性有严格要求的场景允许例外，如多列复杂表单。</p>
<h4 id="建议-标签的使用应尽量简洁，减少不必要的标签。"><a href="#建议-标签的使用应尽量简洁，减少不必要的标签。" class="headerlink" title="[建议] 标签的使用应尽量简洁，减少不必要的标签。"></a>[建议] 标签的使用应尽量简洁，减少不必要的标签。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">class</span>=<span class="string">"avatar"</span> <span class="attr">src</span>=<span class="string">"image.png"</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">"avatar"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">"image.png"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="2-4-属性"><a href="#2-4-属性" class="headerlink" title="2.4 属性"></a>2.4 属性</h3><h4 id="强制-属性名必须使用小写字母。"><a href="#强制-属性名必须使用小写字母。" class="headerlink" title="[强制] 属性名必须使用小写字母。"></a>[强制] 属性名必须使用小写字母。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">cellspacing</span>=<span class="string">"0"</span>&gt;</span>...<span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">cellSpacing</span>=<span class="string">"0"</span>&gt;</span>...<span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h4 id="强制-属性值必须用双引号包围。"><a href="#强制-属性值必须用双引号包围。" class="headerlink" title="[强制] 属性值必须用双引号包围。"></a>[强制] 属性值必须用双引号包围。</h4><p>解释：</p>
<p>不允许使用单引号，不允许不使用引号。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"esl.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">'esl.js'</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">esl.js</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-布尔类型的属性，建议不添加属性值。"><a href="#建议-布尔类型的属性，建议不添加属性值。" class="headerlink" title="[建议] 布尔类型的属性，建议不添加属性值。"></a>[建议] 布尔类型的属性，建议不添加属性值。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">disabled</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">value</span>=<span class="string">"1"</span> <span class="attr">checked</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h4 id="建议-自定义属性建议以-xxx-为前缀，推荐使用-data-。"><a href="#建议-自定义属性建议以-xxx-为前缀，推荐使用-data-。" class="headerlink" title="[建议] 自定义属性建议以 xxx- 为前缀，推荐使用 data-。"></a>[建议] 自定义属性建议以 <code>xxx-</code> 为前缀，推荐使用 <code>data-</code>。</h4><p>解释：</p>
<p>使用前缀有助于区分自定义属性和标准定义的属性。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ol</span> <span class="attr">data-ui-type</span>=<span class="string">"Select"</span>&gt;</span><span class="tag">&lt;/<span class="name">ol</span>&gt;</span></span><br></pre></td></tr></table></figure>




<h2 id="3-通用"><a href="#3-通用" class="headerlink" title="3 通用"></a>3 通用</h2><h3 id="3-1-DOCTYPE"><a href="#3-1-DOCTYPE" class="headerlink" title="3.1 DOCTYPE"></a>3.1 DOCTYPE</h3><h4 id="强制-使用-HTML5-的-doctype-来启用标准模式，建议使用大写的-DOCTYPE。"><a href="#强制-使用-HTML5-的-doctype-来启用标准模式，建议使用大写的-DOCTYPE。" class="headerlink" title="[强制] 使用 HTML5 的 doctype 来启用标准模式，建议使用大写的 DOCTYPE。"></a>[强制] 使用 <code>HTML5</code> 的 <code>doctype</code> 来启用标准模式，建议使用大写的 <code>DOCTYPE</code>。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-启用-IE-Edge-模式。"><a href="#建议-启用-IE-Edge-模式。" class="headerlink" title="[建议] 启用 IE Edge 模式。"></a>[建议] 启用 IE Edge 模式。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"X-UA-Compatible"</span> <span class="attr">content</span>=<span class="string">"IE=Edge"</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-在-html-标签上设置正确的-lang-属性。"><a href="#建议-在-html-标签上设置正确的-lang-属性。" class="headerlink" title="[建议] 在 html 标签上设置正确的 lang 属性。"></a>[建议] 在 <code>html</code> 标签上设置正确的 lang 属性。</h4><p>解释：</p>
<p>有助于提高页面的可访问性，如：让语音合成工具确定其所应该采用的发音，令翻译工具确定其翻译语言等。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"zh-CN"</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h3 id="3-2-编码"><a href="#3-2-编码" class="headerlink" title="3.2 编码"></a>3.2 编码</h3><h4 id="强制-页面必须使用精简形式，明确指定字符编码。指定字符编码的-meta-必须是-head-的第一个直接子元素。"><a href="#强制-页面必须使用精简形式，明确指定字符编码。指定字符编码的-meta-必须是-head-的第一个直接子元素。" class="headerlink" title="[强制] 页面必须使用精简形式，明确指定字符编码。指定字符编码的 meta 必须是 head 的第一个直接子元素。"></a>[强制] 页面必须使用精简形式，明确指定字符编码。指定字符编码的 <code>meta</code> 必须是 <code>head</code> 的第一个直接子元素。</h4><p>解释：</p>
<p>见 <a href="http://www.qianduan.net/html5-charset-can-it.html" target="_blank" rel="noopener">HTML5 Charset能用吗</a> 一文。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">        ......</span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        ......</span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-HTML-文件使用无-BOM-的-UTF-8-编码。"><a href="#建议-HTML-文件使用无-BOM-的-UTF-8-编码。" class="headerlink" title="[建议] HTML 文件使用无 BOM 的 UTF-8 编码。"></a>[建议] <code>HTML</code> 文件使用无 <code>BOM</code> 的 <code>UTF-8</code> 编码。</h4><p>解释：</p>
<p>UTF-8 编码具有更广泛的适应性。BOM 在使用程序或工具处理文件时可能造成不必要的干扰。</p>
<h3 id="3-3-CSS和JavaScript引入"><a href="#3-3-CSS和JavaScript引入" class="headerlink" title="3.3 CSS和JavaScript引入"></a>3.3 CSS和JavaScript引入</h3><h4 id="强制-引入-CSS-时必须指明-rel-quot-stylesheet-quot-。"><a href="#强制-引入-CSS-时必须指明-rel-quot-stylesheet-quot-。" class="headerlink" title="[强制] 引入 CSS 时必须指明 rel=&quot;stylesheet&quot;。"></a>[强制] 引入 <code>CSS</code> 时必须指明 <code>rel=&quot;stylesheet&quot;</code>。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"stylesheet"</span> <span class="attr">src</span>=<span class="string">"page.css"</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h4 id="建议-引入-CSS-和-JavaScript-时无须指明-type-属性。"><a href="#建议-引入-CSS-和-JavaScript-时无须指明-type-属性。" class="headerlink" title="[建议] 引入 CSS 和 JavaScript 时无须指明 type 属性。"></a>[建议] 引入 <code>CSS</code> 和 <code>JavaScript</code> 时无须指明 <code>type</code> 属性。</h4><p>解释：</p>
<p><code>text/css</code> 和 <code>text/javascript</code> 是 type 的默认值。</p>
<h4 id="建议-展现定义放置于外部-CSS-中，行为定义放置于外部-JavaScript-中。"><a href="#建议-展现定义放置于外部-CSS-中，行为定义放置于外部-JavaScript-中。" class="headerlink" title="[建议] 展现定义放置于外部 CSS 中，行为定义放置于外部 JavaScript 中。"></a>[建议] 展现定义放置于外部 <code>CSS</code> 中，行为定义放置于外部 <code>JavaScript</code> 中。</h4><p>解释：</p>
<p>结构-样式-行为的代码分离，对于提高代码的可阅读性和维护性都有好处。</p>
<h4 id="建议-在-head-中引入页面需要的所有-CSS-资源。"><a href="#建议-在-head-中引入页面需要的所有-CSS-资源。" class="headerlink" title="[建议] 在 head 中引入页面需要的所有 CSS 资源。"></a>[建议] 在 <code>head</code> 中引入页面需要的所有 <code>CSS</code> 资源。</h4><p>解释：</p>
<p>在页面渲染的过程中，新的CSS可能导致元素的样式重新计算和绘制，页面闪烁。</p>
<h4 id="建议-JavaScript-应当放在页面末尾，或采用异步加载。"><a href="#建议-JavaScript-应当放在页面末尾，或采用异步加载。" class="headerlink" title="[建议] JavaScript 应当放在页面末尾，或采用异步加载。"></a>[建议] <code>JavaScript</code> 应当放在页面末尾，或采用异步加载。</h4><p>解释：</p>
<p>将 script 放在页面中间将阻断页面的渲染。出于性能方面的考虑，如非必要，请遵守此条建议。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- a lot of elements --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"init-behavior.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h4 id="建议-移动环境或只针对现代浏览器设计的-Web-应用，如果引用外部资源的-URL-协议部分与页面相同，建议省略协议前缀。"><a href="#建议-移动环境或只针对现代浏览器设计的-Web-应用，如果引用外部资源的-URL-协议部分与页面相同，建议省略协议前缀。" class="headerlink" title="[建议] 移动环境或只针对现代浏览器设计的 Web 应用，如果引用外部资源的 URL 协议部分与页面相同，建议省略协议前缀。"></a>[建议] 移动环境或只针对现代浏览器设计的 Web 应用，如果引用外部资源的 <code>URL</code> 协议部分与页面相同，建议省略协议前缀。</h4><p>解释：</p>
<p>使用 <code>protocol-relative URL</code> 引入 CSS，在 <code>IE7/8</code> 下，会发两次请求。是否使用 <code>protocol-relative URL</code> 应充分考虑页面针对的环境。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"//s1.bdstatic.com/cache/static/jquery-1.10.2.min_f2fb5194.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>






<h2 id="4-head"><a href="#4-head" class="headerlink" title="4 head"></a>4 head</h2><h3 id="4-1-title"><a href="#4-1-title" class="headerlink" title="4.1 title"></a>4.1 title</h3><h4 id="强制-页面必须包含-title-标签声明标题。"><a href="#强制-页面必须包含-title-标签声明标题。" class="headerlink" title="[强制] 页面必须包含 title 标签声明标题。"></a>[强制] 页面必须包含 <code>title</code> 标签声明标题。</h4><h4 id="强制-title-必须作为-head-的直接子元素，并紧随-charset-声明之后。"><a href="#强制-title-必须作为-head-的直接子元素，并紧随-charset-声明之后。" class="headerlink" title="[强制] title 必须作为 head 的直接子元素，并紧随 charset 声明之后。"></a>[强制] <code>title</code> 必须作为 <code>head</code> 的直接子元素，并紧随 <code>charset</code> 声明之后。</h4><p>解释：</p>
<p>title 中如果包含 ascii 之外的字符，浏览器需要知道字符编码类型才能进行解码，否则可能导致乱码。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>页面标题<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="4-2-favicon"><a href="#4-2-favicon" class="headerlink" title="4.2 favicon"></a>4.2 favicon</h3><h4 id="强制-保证-favicon-可访问。"><a href="#强制-保证-favicon-可访问。" class="headerlink" title="[强制] 保证 favicon 可访问。"></a>[强制] 保证 <code>favicon</code> 可访问。</h4><p>解释：</p>
<p>在未指定 favicon 时，大多数浏览器会请求 Web Server 根目录下的 favicon.ico 。为了保证favicon可访问，避免404，必须遵循以下两种方法之一：</p>
<ol>
<li>在 Web Server 根目录放置 favicon.ico 文件。</li>
<li>使用 link 指定 favicon。</li>
</ol>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"shortcut icon"</span> <span class="attr">href</span>=<span class="string">"path/to/favicon.ico"</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="4-3-viewport"><a href="#4-3-viewport" class="headerlink" title="4.3 viewport"></a>4.3 viewport</h3><h4 id="建议-若页面欲对移动设备友好，需指定页面的-viewport。"><a href="#建议-若页面欲对移动设备友好，需指定页面的-viewport。" class="headerlink" title="[建议] 若页面欲对移动设备友好，需指定页面的 viewport。"></a>[建议] 若页面欲对移动设备友好，需指定页面的 <code>viewport</code>。</h4><p>解释：</p>
<p>viewport meta tag可以设置可视区域的宽度和初始缩放大小，避免在移动设备上出现页面展示不正常。</p>
<p>比如，在页面宽度小于 980px 时，若需 iOS 设备友好，应当设置 viewport 的 width 值来适应你的页面宽度。同时因为不同移动设备分辨率不同，在设置时，应当使用 device-width 和 device-height 变量。</p>
<p>另外，为了使 viewport 正常工作，在页面内容样式布局设计上也要做相应调整，如避免绝对定位等。关于 viewport 的更多介绍，可以参见 <a href="https://developer.apple.com/library/mac/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html#//apple_ref/doc/uid/TP40006509-SW26" target="_blank" rel="noopener">Safari Web Content Guide的介绍</a></p>
<h2 id="5-图片"><a href="#5-图片" class="headerlink" title="5 图片"></a>5 图片</h2><h4 id="强制-禁止-img-的-src-取值为空。延迟加载的图片也要增加默认的-src。"><a href="#强制-禁止-img-的-src-取值为空。延迟加载的图片也要增加默认的-src。" class="headerlink" title="[强制] 禁止 img 的 src 取值为空。延迟加载的图片也要增加默认的 src。"></a>[强制] 禁止 <code>img</code> 的 <code>src</code> 取值为空。延迟加载的图片也要增加默认的 <code>src</code>。</h4><p>解释：</p>
<p>src 取值为空，会导致部分浏览器重新加载一次当前页面，参考：<a href="https://developer.yahoo.com/performance/rules.html#emptysrc" target="_blank" rel="noopener">https://developer.yahoo.com/performance/rules.html#emptysrc</a></p>
<h4 id="建议-避免为-img-添加不必要的-title-属性。"><a href="#建议-避免为-img-添加不必要的-title-属性。" class="headerlink" title="[建议] 避免为 img 添加不必要的 title 属性。"></a>[建议] 避免为 <code>img</code> 添加不必要的 <code>title</code> 属性。</h4><p>解释：</p>
<p>多余的 title 影响看图体验，并且增加了页面尺寸。</p>
<h4 id="建议-为重要图片添加-alt-属性。"><a href="#建议-为重要图片添加-alt-属性。" class="headerlink" title="[建议] 为重要图片添加 alt 属性。"></a>[建议] 为重要图片添加 <code>alt</code> 属性。</h4><p>解释：</p>
<p>可以提高图片加载失败时的用户体验。</p>
<h4 id="建议-添加-width-和-height-属性，以避免页面抖动。"><a href="#建议-添加-width-和-height-属性，以避免页面抖动。" class="headerlink" title="[建议] 添加 width 和 height 属性，以避免页面抖动。"></a>[建议] 添加 <code>width</code> 和 <code>height</code> 属性，以避免页面抖动。</h4><h4 id="建议-有下载需求的图片采用-img-标签实现，无下载需求的图片采用-CSS-背景图实现。"><a href="#建议-有下载需求的图片采用-img-标签实现，无下载需求的图片采用-CSS-背景图实现。" class="headerlink" title="[建议] 有下载需求的图片采用 img 标签实现，无下载需求的图片采用 CSS 背景图实现。"></a>[建议] 有下载需求的图片采用 <code>img</code> 标签实现，无下载需求的图片采用 <code>CSS</code> 背景图实现。</h4><p>解释：</p>
<ol>
<li>产品 logo、用户头像、用户产生的图片等有潜在下载需求的图片，以 img 形式实现，能方便用户下载。</li>
<li>无下载需求的图片，比如：icon、背景、代码使用的图片等，尽可能采用 css 背景图实现。</li>
</ol>
<h2 id="6-表单"><a href="#6-表单" class="headerlink" title="6 表单"></a>6 表单</h2><h3 id="6-1-控件标题"><a href="#6-1-控件标题" class="headerlink" title="6.1 控件标题"></a>6.1 控件标题</h3><h4 id="强制-有文本标题的控件必须使用-label-标签将其与其标题相关联。"><a href="#强制-有文本标题的控件必须使用-label-标签将其与其标题相关联。" class="headerlink" title="[强制] 有文本标题的控件必须使用 label 标签将其与其标题相关联。"></a>[强制] 有文本标题的控件必须使用 <code>label</code> 标签将其与其标题相关联。</h4><p>解释：</p>
<p>有两种方式：</p>
<ol>
<li>将控件置于 label 内。</li>
<li>label 的 for 属性指向控件的 id。</li>
</ol>
<p>推荐使用第一种，减少不必要的 id。如果 DOM 结构不允许直接嵌套，则应使用第二种。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">label</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">name</span>=<span class="string">"confirm"</span> <span class="attr">value</span>=<span class="string">"on"</span>&gt;</span> 我已确认上述条款<span class="tag">&lt;/<span class="name">label</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">label</span> <span class="attr">for</span>=<span class="string">"username"</span>&gt;</span>用户名：<span class="tag">&lt;/<span class="name">label</span>&gt;</span> <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"textbox"</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">id</span>=<span class="string">"username"</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h3 id="6-2-按钮"><a href="#6-2-按钮" class="headerlink" title="6.2 按钮"></a>6.2 按钮</h3><h4 id="强制-使用-button-元素时必须指明-type-属性值。"><a href="#强制-使用-button-元素时必须指明-type-属性值。" class="headerlink" title="[强制] 使用 button 元素时必须指明 type 属性值。"></a>[强制] 使用 <code>button</code> 元素时必须指明 <code>type</code> 属性值。</h4><p>解释：</p>
<p>button 元素的默认 type 为 submit，如果被置于 form 元素中，点击后将导致表单提交。为显示区分其作用方便理解，必须给出 type 属性。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"submit"</span>&gt;</span>提交<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span>&gt;</span>取消<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-尽量不要使用按钮类元素的-name-属性。"><a href="#建议-尽量不要使用按钮类元素的-name-属性。" class="headerlink" title="[建议] 尽量不要使用按钮类元素的 name 属性。"></a>[建议] 尽量不要使用按钮类元素的 <code>name</code> 属性。</h4><p>解释：</p>
<p>由于浏览器兼容性问题，使用按钮的 name 属性会带来许多难以发现的问题。具体情况可参考<a href="http://w3help.org/zh-cn/causes/CM2001" target="_blank" rel="noopener">此文</a>。</p>
<h3 id="6-3-可访问性-A11Y"><a href="#6-3-可访问性-A11Y" class="headerlink" title="6.3 可访问性 (A11Y)"></a>6.3 可访问性 (A11Y)</h3><h4 id="建议-负责主要功能的按钮在-DOM-中的顺序应靠前。"><a href="#建议-负责主要功能的按钮在-DOM-中的顺序应靠前。" class="headerlink" title="[建议] 负责主要功能的按钮在 DOM 中的顺序应靠前。"></a>[建议] 负责主要功能的按钮在 <code>DOM</code> 中的顺序应靠前。</h4><p>解释：</p>
<p>负责主要功能的按钮应相对靠前，以提高可访问性。如果在 CSS 中指定了 <code>float: right</code> 则可能导致视觉上主按钮在前，而 DOM 中主按钮靠后的情况。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="css"><span class="selector-class">.buttons</span> <span class="selector-class">.button-group</span> &#123;</span></span><br><span class="line">    float: right;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"buttons"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"button-group"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"submit"</span>&gt;</span>提交<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span>&gt;</span>取消<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="css"><span class="selector-class">.buttons</span> <span class="selector-tag">button</span> &#123;</span></span><br><span class="line">    float: right;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"buttons"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span>&gt;</span>取消<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"submit"</span>&gt;</span>提交<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-当使用-JavaScript-进行表单提交时，如果条件允许，应使原生提交功能正常工作。"><a href="#建议-当使用-JavaScript-进行表单提交时，如果条件允许，应使原生提交功能正常工作。" class="headerlink" title="[建议] 当使用 JavaScript 进行表单提交时，如果条件允许，应使原生提交功能正常工作。"></a>[建议] 当使用 <code>JavaScript</code> 进行表单提交时，如果条件允许，应使原生提交功能正常工作。</h4><p>解释：</p>
<p>当浏览器 JS 运行错误或关闭 JS 时，提交功能将无法工作。如果正确指定了 form 元素的 action 属性和表单控件的 name 属性时，提交仍可继续进行。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">"/login"</span> <span class="attr">method</span>=<span class="string">"post"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">placeholder</span>=<span class="string">"用户名"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">type</span>=<span class="string">"password"</span> <span class="attr">placeholder</span>=<span class="string">"密码"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-在针对移动设备开发的页面时，根据内容类型指定输入框的-type-属性。"><a href="#建议-在针对移动设备开发的页面时，根据内容类型指定输入框的-type-属性。" class="headerlink" title="[建议] 在针对移动设备开发的页面时，根据内容类型指定输入框的 type 属性。"></a>[建议] 在针对移动设备开发的页面时，根据内容类型指定输入框的 <code>type</code> 属性。</h4><p>解释：</p>
<p>根据内容类型指定输入框类型，能获得能友好的输入体验。</p>
<p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"date"</span>&gt;</span></span><br></pre></td></tr></table></figure>





<h2 id="7-多媒体"><a href="#7-多媒体" class="headerlink" title="7 多媒体"></a>7 多媒体</h2><h4 id="建议-当在现代浏览器中使用-audio-以及-video-标签来播放音频、视频时，应当注意格式。"><a href="#建议-当在现代浏览器中使用-audio-以及-video-标签来播放音频、视频时，应当注意格式。" class="headerlink" title="[建议] 当在现代浏览器中使用 audio 以及 video 标签来播放音频、视频时，应当注意格式。"></a>[建议] 当在现代浏览器中使用 <code>audio</code> 以及 <code>video</code> 标签来播放音频、视频时，应当注意格式。</h4><p>解释：</p>
<p>音频应尽可能覆盖到如下格式：</p>
<ul>
<li>MP3</li>
<li>WAV</li>
<li>Ogg</li>
</ul>
<p>视频应尽可能覆盖到如下格式：</p>
<ul>
<li>MP4</li>
<li>WebM</li>
<li>Ogg</li>
</ul>
<h4 id="建议-在支持-HTML5-的浏览器中优先使用-audio-和-video-标签来定义音视频元素。"><a href="#建议-在支持-HTML5-的浏览器中优先使用-audio-和-video-标签来定义音视频元素。" class="headerlink" title="[建议] 在支持 HTML5 的浏览器中优先使用 audio 和 video 标签来定义音视频元素。"></a>[建议] 在支持 <code>HTML5</code> 的浏览器中优先使用 <code>audio</code> 和 <code>video</code> 标签来定义音视频元素。</h4><h4 id="建议-使用退化到插件的方式来对多浏览器进行支持。"><a href="#建议-使用退化到插件的方式来对多浏览器进行支持。" class="headerlink" title="[建议] 使用退化到插件的方式来对多浏览器进行支持。"></a>[建议] 使用退化到插件的方式来对多浏览器进行支持。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">audio</span> <span class="attr">controls</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">source</span> <span class="attr">src</span>=<span class="string">"audio.mp3"</span> <span class="attr">type</span>=<span class="string">"audio/mpeg"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">source</span> <span class="attr">src</span>=<span class="string">"audio.ogg"</span> <span class="attr">type</span>=<span class="string">"audio/ogg"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">object</span> <span class="attr">width</span>=<span class="string">"100"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">data</span>=<span class="string">"audio.mp3"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">embed</span> <span class="attr">width</span>=<span class="string">"100"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">src</span>=<span class="string">"audio.swf"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">object</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">audio</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">video</span> <span class="attr">width</span>=<span class="string">"100"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">controls</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">source</span> <span class="attr">src</span>=<span class="string">"video.mp4"</span> <span class="attr">type</span>=<span class="string">"video/mp4"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">source</span> <span class="attr">src</span>=<span class="string">"video.ogg"</span> <span class="attr">type</span>=<span class="string">"video/ogg"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">object</span> <span class="attr">width</span>=<span class="string">"100"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">data</span>=<span class="string">"video.mp4"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">embed</span> <span class="attr">width</span>=<span class="string">"100"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">src</span>=<span class="string">"video.swf"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">object</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">video</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-只在必要的时候开启音视频的自动播放。"><a href="#建议-只在必要的时候开启音视频的自动播放。" class="headerlink" title="[建议] 只在必要的时候开启音视频的自动播放。"></a>[建议] 只在必要的时候开启音视频的自动播放。</h4><h4 id="建议-在-object-标签内部提供指示浏览器不支持该标签的说明。"><a href="#建议-在-object-标签内部提供指示浏览器不支持该标签的说明。" class="headerlink" title="[建议] 在 object 标签内部提供指示浏览器不支持该标签的说明。"></a>[建议] 在 <code>object</code> 标签内部提供指示浏览器不支持该标签的说明。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">object</span> <span class="attr">width</span>=<span class="string">"100"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">data</span>=<span class="string">"something.swf"</span>&gt;</span>DO NOT SUPPORT THIS TAG<span class="tag">&lt;/<span class="name">object</span>&gt;</span></span><br></pre></td></tr></table></figure>




<h2 id="8-模板中的-HTML"><a href="#8-模板中的-HTML" class="headerlink" title="8 模板中的 HTML"></a>8 模板中的 HTML</h2><h4 id="建议-模板代码的缩进优先保证-HTML-代码的缩进规则。"><a href="#建议-模板代码的缩进优先保证-HTML-代码的缩进规则。" class="headerlink" title="[建议] 模板代码的缩进优先保证 HTML 代码的缩进规则。"></a>[建议] 模板代码的缩进优先保证 <code>HTML</code> 代码的缩进规则。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line">&#123;if $display == true&#125;</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    &#123;foreach $item_list as $item&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;$item.name&#125;<span class="tag">&lt;<span class="name">li</span>&gt;</span></span><br><span class="line">    &#123;/foreach&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">&#123;/if&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line">&#123;if $display == true&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    &#123;foreach $item_list as $item&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;$item.name&#125;<span class="tag">&lt;<span class="name">li</span>&gt;</span></span><br><span class="line">    &#123;/foreach&#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">&#123;/if&#125;</span><br></pre></td></tr></table></figure>

<h4 id="建议-模板代码应以保证-HTML-单个标签语法的正确性为基本原则。"><a href="#建议-模板代码应以保证-HTML-单个标签语法的正确性为基本原则。" class="headerlink" title="[建议] 模板代码应以保证 HTML 单个标签语法的正确性为基本原则。"></a>[建议] 模板代码应以保证 <code>HTML</code> 单个标签语法的正确性为基本原则。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">"&#123;if $item.type_id == $current_type&#125;focus&#123;/if&#125;"</span>&gt;</span>&#123; $item.type_name &#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">li</span> &#123;<span class="attr">if</span> $<span class="attr">item.type_id</span> == <span class="string">$current_type&#125;</span> <span class="attr">class</span>=<span class="string">"focus"</span>&#123;/<span class="attr">if</span>&#125;&gt;</span>&#123; $item.type_name &#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="建议-在循环处理模板数据构造表格时，若要求每行输出固定的个数，建议先将数据分组，之后再循环输出。"><a href="#建议-在循环处理模板数据构造表格时，若要求每行输出固定的个数，建议先将数据分组，之后再循环输出。" class="headerlink" title="[建议] 在循环处理模板数据构造表格时，若要求每行输出固定的个数，建议先将数据分组，之后再循环输出。"></a>[建议] 在循环处理模板数据构造表格时，若要求每行输出固定的个数，建议先将数据分组，之后再循环输出。</h4><p>示例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- good --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span>&gt;</span></span><br><span class="line">    &#123;foreach $item_list as $item_group&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        &#123;foreach $item_group as $item&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123; $item.name &#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        &#123;/foreach&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">    &#123;/foreach&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- bad --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">    &#123;foreach $item_list as $item&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123; $item.name &#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        &#123;if $item@iteration is div by 5&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        &#123;/if&#125;</span><br><span class="line">    &#123;/foreach&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>





      
      <!-- reward -->
      
      <div id="reward-btn">
        Donate
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>Copyright： </strong>
              Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://bodboy.gitee.io/blog/2018/12/02/html-standard/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/H5/" rel="tag">H5</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/blog/2018/12/15/js-standard/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            JavaScript编码规范
          
        </div>
      </a>
    
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'uW8QYGvvhWmNWdiwpznJ0GVa-gzGzoHsz',
        app_key: 'LrJhMzx9qEwcMV1DJjTI6f6C',
        path: window.location.pathname,
        notify: 'false',
        verify: 'false',
        avatar: '[object Object]',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2021
        <i class="ri-heart-fill heart_icon"></i> panda
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        Powered by <a href="https://hexo.io" target="_blank">Hexo</a>
        <span class="division">|</span>
        Theme - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/blog/"><img src="https://portrait.gitee.com/uploads/avatars/user/1007/3023420_bodboy_1578973210.png!avatar200" alt="小熊的博客"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/tags/%E6%97%85%E8%A1%8C/">旅行</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/blog/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/blog/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/blog/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/blog/js/jquery-2.0.3.min.js"></script>


<script src="/blog/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/blog/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/blog/dist/main.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>


<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>




<script src="/blog/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>

  
<script src="/blog/js/clickLove.js"></script>



<!-- 复制 -->



    
    <div id="music">
    
    
    
    <iframe frameborder="no" border="1" marginwidth="0" marginheight="0" width="200" height="52"
        src="//music.163.com/outchain/player?type=2&id=1297802566&auto=1&height=32"></iframe>
</div>

<style>
    #music {
        position: fixed;
        right: 15px;
        bottom: 0;
        z-index: 998;
    }
</style>
    
  </div>
</body>

</html>